package view.game
{
	//手牌
	
	import event.GlobalEvent;
	
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import flash3.framework.Map;
	
	import util.Graphics;
	
	import view.plugin.SelectPanel;

	public class CardControl extends Sprite
	{
		public static const enXLeft:uint		= 0;						//左对齐
		public static const enXCenter:uint		= 1;						//中对齐
		public static const enXRight:uint		= 2;						//右对齐

		public static const enYTop:uint			= 0;							//上对齐
		public static const enYCenter:uint		= 1;						//中对齐
		public static const enYBottom:uint		= 2;						//下对齐

		private var bDisplay:Boolean;							//显示标志
		private var bHorizontal:Boolean = true;				//横放标志
		private var bClick:Boolean = true;					//可以点击
		private var bRectSelct:Boolean = false;				//是否可以框选

		private var dwCardHSpace:uint = 20;						//横向间隔
		private var dwCardVSpace:uint = 20;						//竖向间隔
		private var dwShootAltitude:uint = 20;					//弹起高度

		private var benchmarkPos:Point;						//基准位置
		private var xCollocateMode:uint = enXCenter;					//显示模式
		private var yCollocateMode:uint = enYCenter;					//显示模式
		
		private var cardDataItem:Array=[];						//扑克数据
		private var cardWidth:uint;	
		private var cardHeight:uint;							//扑克大小
		
		private var displayArea:Rectangle;
		
		private var cardBitmapData:CardBitmapData
		
		private var bBright:Boolean;						//是否明牌
		
		private var cardImageMap:Map = new Map();
		private var backImageMap:Map = new Map();
		
		private var curSortType:uint = GameLogic.ST_ORDER;
		
		private var yPos:Number = 0;
		
		private var bReverse:Boolean = false;
		
		public function CardControl(click:Boolean = true):void
		{
			bClick = click;
		}
		
		public function initialize(cardData:Array, cardBitmapData:CardBitmapData, oriPos:Point, bHorizontal:Boolean, bBright:Boolean, sortType:uint):void
		{
			this.bBright = bBright;
			this.cardBitmapData = cardBitmapData;
			this.benchmarkPos = oriPos;
			this.bHorizontal = bHorizontal;
			this.cardHeight = cardBitmapData.cardHeight;
			this.cardWidth = cardBitmapData.cardWidth;
			this.curSortType = sortType;
			SetCardData(cardData);
		}
		
		public function setBright(bBright:Boolean):void
		{
			this.bBright = bBright;
		}
		public function get isBright():Boolean
		{
			return bBright;
		}
		public function setRectSelect(rectSelect:Boolean):void
		{
			this.bRectSelct = rectSelect;
			if(rectSelect)
				SelectPanel.i.addEventListener(GlobalEvent.MOUSE_UP, onMouseUp);
			else
				SelectPanel.i.removeEventListener(GlobalEvent.MOUSE_UP, onMouseUp);
		}
		
		private function onMouseUp(evt:GlobalEvent):void
		{
			rectSelect(evt.data as Rectangle);
		}
		
		//弹起扑克
		public function ShootAllCard(bShoot:Boolean):void
		{
			var bRectify:Boolean=false;
			var pCardItem:CardItem  = null;
			var dwCardCount:uint =cardDataItem.length;
			
			for (var i:uint=0;i<dwCardCount;i++)
			{
				pCardItem=cardDataItem[i];
				if (pCardItem.bShoot!=bShoot)
				{
					this.setShootCard(i, bShoot);
				}
			}
		}
		//设置间距
		public function SetCardSpace(dwCardHSpace:uint, dwCardVSpace:uint, dwShootAltitude:uint):void
		{
			this.dwCardHSpace=dwCardHSpace;
			this.dwCardVSpace=dwCardVSpace;
			this.dwShootAltitude=dwShootAltitude;
		}
		public function setBitmapData(bitmapData:CardBitmapData, w:Number=0, h:Number=0):void
		{
			this.cardBitmapData = bitmapData;
			this.cardHeight = h == 0 ? cardBitmapData.cardHeight : h;
			this.cardWidth = w == 0 ? cardBitmapData.cardWidth : w;
		}
		//获取数目
		public function GetCardCount():uint
		{
			return cardDataItem.length;	
		}
		//设置扑克
		public function SetCardData(bCardData:Array):void
		{
			//设置扑克
			if(bCardData == null || bCardData.length == 0)
				cardDataItem.length = 0;
			else
			{
				GameLogic.i.SortCardList(bCardData, curSortType);
				var cardItem:CardItem = null;
				cardDataItem.splice(bCardData.length-1);
				for (var i:uint=0;i<bCardData.length;i++)
				{
					cardItem = cardDataItem[i] ? (cardDataItem[i] as CardItem) : new CardItem();
					cardItem.bShoot=false;
					cardItem.bCardData=bCardData[i];
					cardDataItem[i] = cardItem;
				}
			}
			
			drawCard();
			rectifyControl();
		}
		public function setDirection(bHorizontal:Boolean):void
		{
			this.bHorizontal = bHorizontal;
		}
		//设置间距
		public function setCardSpace(dwCardHSpace:Number, dwCardVSpace:Number, dwShootAltitude:Number):void
		{
			//设置变量
			dwCardHSpace=dwCardHSpace;
			dwCardVSpace=dwCardVSpace;
			dwShootAltitude=dwShootAltitude;
		}
		//基准位置
		public function setBenchmarkPos(BenchmarkPos:Point, XCollocateMode:uint, YCollocateMode:uint):void
		{
			//设置变量
			benchmarkPos=BenchmarkPos;
			xCollocateMode=XCollocateMode;
			yCollocateMode=YCollocateMode
		}
		//获取扑克
		public function GetCardData():Array
		{
			var retArr:Array = [];
			for(var i:uint = 0; i < cardDataItem.length; i++)
			{
				var cardItem:CardItem = cardDataItem[i];
				retArr.push(cardItem.bCardData);
			}
			return retArr;
		}
		//获取扑克
		public function getShootCard():Array
		{
			//变量定义
			var bShootCount:uint=0;
			var dwCardCount:uint=GetCardCount();
			var bCardData:Array = [];
			
			//搜索扑克
			var pCardItem:CardItem = null;
			for (var i:uint=0;i<dwCardCount;i++)
			{
				//获取扑克
				pCardItem=cardDataItem[i];
				
				//扑克分析
				if (pCardItem.bShoot) 
					bCardData[bShootCount++]=pCardItem.bCardData;
			}
			return bCardData;
		}
		public function removeShoottCard():void
		{
			var dwCardCount:uint=cardDataItem.length;
			var curIndex:uint = 0;
			var cardItem:CardItem = null;
			for(var i:int = 0; i < dwCardCount-curIndex; i++)
			{
				cardItem = cardDataItem[i];
				if(cardItem.bShoot)
				{
					removeCardByIndex(i);
					i -= 1;
					curIndex++;
				}
			}
			rectifyControl();
		}
		public function removeCard(cardData:Array):void
		{
			var curIndex1:uint = 0;
			var curIndex2:uint = 0;
			var dwCardCount:uint=cardDataItem.length;
			var dwRemoveCardCount:uint = cardData.length;
			for(var i:int = 0; i < dwCardCount-curIndex1; i++)
			{
				for(var j:int = 0; j < dwRemoveCardCount-curIndex2; j++)
				{
					if(cardDataItem[i].bCardData == cardData[j])
					{
						removeCardByIndex(i);
						cardData.splice(j, 1);
						i -= 1;
						curIndex1++;
						curIndex2++;
						break;
					}
				}
				if(cardData.length == 0)
					break;
			}
			rectifyControl();
		}
		public function getSortType():uint
		{
			return curSortType;
		}
		public function setSortType(sortType:uint):void
		{
			curSortType = sortType;
		}
		public function sortCard(type:uint):void
		{
			curSortType = type;
			var cardData:Array = GetCardData();
			SetCardData(cardData);
		}
		public function removeCardByIndex(index:uint):void
		{
			var cardItem:CardItem = cardDataItem[index];
			if(bClick)
				cardItem.movieClip.removeEventListener(MouseEvent.CLICK, onCardClick);
			removeChild(cardItem.movieClip);
			cardDataItem.splice(index, 1);
		}

		//调整位置
		public function rectifyControl():void
		{
			//变量定义
			var dwCardCount:uint=cardDataItem.length;
			
			//计算大小
			var startX:Number;
			var startY:Number;
			var height:Number;
			var width:Number;

			if (bHorizontal==true)
			{
				height = cardHeight + dwShootAltitude;
				width = (dwCardCount>0)?(cardWidth+(dwCardCount-1)*dwCardHSpace):0;
			}
			else
			{
				width  = cardWidth;
				height = (dwCardCount>0)?(cardHeight+(dwCardCount-1)*dwCardVSpace):0;
			}
			
			//横向位置
			var nXPos:Number = 0;
			switch (xCollocateMode)
			{
				case enXLeft:	{ nXPos=benchmarkPos.x; break; }
				case enXCenter: { nXPos=benchmarkPos.x-width/2; break; }
				case enXRight:	{ nXPos=benchmarkPos.x-width; break; }
			}
			
			var nYPos:Number = 0;
			switch (yCollocateMode)
			{
				case enYTop:	{ nYPos=benchmarkPos.y; break; }
				case enYCenter: { nYPos=yPos=benchmarkPos.y-height/2; break; }
				case enYBottom: { nYPos=benchmarkPos.y-height; break; }
			}
			
			for(var i:uint = 0;i < dwCardCount; i++)
			{
				if(bHorizontal)
				{
					cardDataItem[i].movieClip.x = nXPos + i * dwCardHSpace;
					cardDataItem[i].movieClip.y = nYPos + 0;
				}
				else
				{
					cardDataItem[i].movieClip.x = nXPos;
					cardDataItem[i].movieClip.y = nYPos + i * dwCardVSpace;
				}
			}
		}
		
		public function isReDraw():Boolean
		{
			return this.visible;
		}
		public function setReverse(reverse:Boolean):void
		{
			bReverse = reverse;
		}
		public function drawCard():void
		{
			var movieClip:MovieClip = null;
			while(numChildren > 0)
			{
				movieClip = this.getChildAt(0) as MovieClip;
				if(bClick)
					movieClip.removeEventListener(MouseEvent.CLICK, onCardClick);
				removeChild(movieClip);
			}
			var index:uint = 0;
			for (var i:uint=0;i<cardDataItem.length;i++)
			{
				index = bReverse ? cardDataItem.length-i-1 : i;
				movieClip = getCardMovieClip(cardDataItem[index].bCardData);
				if(bClick)
					movieClip.addEventListener(MouseEvent.CLICK, onCardClick);
				cardDataItem[index].movieClip = movieClip;
				addChild(movieClip);
			}
		}
		public function reDrawCard():void
		{
			var movieClip:MovieClip = null;
			var index:uint = 0;
			for (var i:uint=0;i<cardDataItem.length;i++)
			{
				index = bReverse ? cardDataItem.length-i-1 : i;
				movieClip = cardDataItem[index].movieClip;
				if(bClick)
					movieClip.addEventListener(MouseEvent.CLICK, onCardClick);
				addChild(movieClip);
			}
		}
		
		public function getCardMovieClip(data:uint):MovieClip
		{
			var cardMap:Map = bBright ? cardImageMap : backImageMap;
			
			if(cardMap.containsKey(data))
				return cardMap.getValue(data) as MovieClip;
			
			var mc:MovieClip = cardBitmapData.getMovieClip(data, bBright);
			mc.width = this.cardWidth;
			mc.height = this.cardHeight;
			cardMap.put(data, mc);
			
			return mc;
		}
		
		private function onCardClick(evt:MouseEvent):void
		{
			var cardItem:CardItem = null;
			for(var i:uint = 0; i < cardDataItem.length; i++)
			{
				cardItem = cardDataItem[i];
				if(cardItem.movieClip == evt.target)
				{
					setShootCard(i, !cardItem.bShoot);
					break;
				}
			}
		}
		
		public function setShootCard(index:*, isShoot:Boolean):void
		{
			var cardItem:CardItem = cardDataItem[index];
			cardItem.bShoot = isShoot;
//			if(!cardItem.bShoot && Math.floor(cardItem.movieClip.y) == yPos)
//				return;
//			cardItem.movieClip.y = cardItem.bShoot ? cardItem.movieClip.y-dwShootAltitude : cardItem.movieClip.y+dwShootAltitude;
			cardItem.movieClip.y = cardItem.bShoot ? yPos-dwShootAltitude : yPos;
		}
		
		public function shootCardByData(cardData:Array):void
		{
			//变量定义
			var bChangeStatus:Boolean=false;
			
			var cbCardData:Array = cardData.slice();
			
			//收起扑克
			var i:int = 0;
			for (i=0;i<cardDataItem.length;i++) 
			{
				if (cardDataItem[i].bShoot==true)
				{
					setShootCard(i, false);
				}
			}
			
			//弹起扑克
			var j:uint = 0;
			var count:uint = 0;
			var cardCount:uint = cbCardData.length;
			for (i=0;i<cardCount-count; i++)	
			{
				for (j=0;j<cardDataItem.length;j++)
				{
					if (cardDataItem[j].bCardData==cbCardData[i]) 
					{
						setShootCard(j, true);
						cbCardData.splice(i, 1);
						i -= 1;
						count++;
						break;
					}
				}
			}
		}
		
		public function getMovieClipList():Array
		{
			var arr:Array = [];
			for(var i:uint = 0; i < cardDataItem.length; i++)
				arr.push(cardDataItem[i].movieClip);
			return arr;
		}
		
		public function getCardCount():uint
		{
			return cardDataItem.length;
		}
		
		public function rectSelect(rect:Rectangle):void
		{
			if(rect.isEmpty())
				return;
			
			var movieClip:MovieClip = null;
			var spriteRect:Rectangle = new Rectangle();
			var spritePoint:Point = null;
			for(var i:uint = 0; i < cardDataItem.length; i++)
			{
				movieClip = cardDataItem[i].movieClip;
				spritePoint = localToGlobal(new Point(movieClip.x, movieClip.y))
				spriteRect.x = spritePoint.x;
				spriteRect.y = spritePoint.y;
				spriteRect.width = (i==cardDataItem.length-1)?movieClip.width:dwCardHSpace;
				spriteRect.height = movieClip.height;
				if(rect.intersects(spriteRect))
				{
					this.setShootCard(i, !cardDataItem[i].bShoot)
				}
			}
		}
	}
}